package com.xuanwei.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwManagementRoleService;
import com.xuanwei.core.util.ParamUtil;
import com.xuanwei.dao.entity.XwManagementRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2019-04-14
 */
@RestController
@RequestMapping("api/role")
public class ManagementRoleController {

    @Autowired
    private IXwManagementRoleService roleService;

    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ResultVO add(String name, String authorities) {
        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("用户角色不能为空");
        }

        if (!ParamUtil.checkIsString(authorities)) {
            return ResultVO.failResult("用户角色权限不能为空");
        }
        String[] strings = authorities.split("-");
        if (strings.length == 0) {
            return ResultVO.failResult("用户角色权限不能为空");
        }
        Integer[] ids = new Integer[strings.length];
        try {
            for (int i = 0; i < strings.length; i++) {
                ids[i] = Integer.parseInt(strings[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failResult("用户角色权限数据错误");
        }
        try {
            roleService.add(name, ids);
            return ResultVO.successResult("添加成功");
        } catch (XwException e) {
            return ResultVO.failResult(e.getMessage());
        }

    }

    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public ResultVO update(Integer id, String name, String authorities) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("用户角色ID不能为空");
        }

        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("用户角色不能为空");
        }

        if (!ParamUtil.checkIsString(authorities)) {
            return ResultVO.failResult("用户角色权限不能为空");
        }
        String[] strings = authorities.split("-");
        if (strings.length == 0) {
            return ResultVO.failResult("用户角色权限不能为空");
        }
        Integer[] ids = new Integer[strings.length];
        try {
            for (int i = 0; i < strings.length; i++) {
                ids[i] = Integer.parseInt(strings[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failResult("用户角色权限数据错误");
        }
        try {
            roleService.update(id, name, ids);
            return ResultVO.successResult("修改成功");
        } catch (XwException e) {
            return ResultVO.failResult(e.getMessage());
        }

    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("用户角色ID不能为空");
        }
        try {
            roleService.delete(id);
            return ResultVO.successResult("删除成功");
        } catch (XwException e) {
            return ResultVO.failResult(e.getMessage());
        }
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list(Integer index, Integer size) {
        if (!ParamUtil.checkIsPositiveInteger(index)) {
            index = 1;
        }
        if (!ParamUtil.checkIsPositiveInteger(size)) {
            size = 20;
        }
        Page<XwManagementRole> page = new Page<>(index, size);
        roleService.list(page);
        return ResultVO.successResult(page);
    }

    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    public ResultVO detail(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("用户角色ID不能为空");
        }
        try {
            return ResultVO.successResult(roleService.detail(id));
        } catch (XwException e) {
            return ResultVO.failResult(e.getMessage());
        }
    }

    @RequestMapping(value = "permissionList.do", method = RequestMethod.GET)
    public ResultVO permissionList() {

        return ResultVO.successResult(roleService.permissionList());
    }

    @RequestMapping(value = "roleList.do", method = RequestMethod.GET)
    public ResultVO roleList() {

        return ResultVO.successResult(roleService.roleList());
    }

}
